﻿// SDTDlg.cpp: 实现文件
//

#include "pch.h"
#include "3180101584SX1.h"
#include "SDTDlg.h"
#include "afxdialogex.h"

extern OpenImageClass OImageC;
// SDTDlg 对话框

IMPLEMENT_DYNAMIC(SDTDlg, CDialog)

SDTDlg::SDTDlg(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_DIALOG_SDT, pParent)
{

}

SDTDlg::~SDTDlg()
{
}

void SDTDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_Y, m_cby);
	DDX_Control(pDX, IDC_COMBO_X, m_cbx);
	DDX_Control(pDX, IDC_CHECK1, m_check);
}


BEGIN_MESSAGE_MAP(SDTDlg, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_LAYOUT, &SDTDlg::OnBnClickedButtonLayout)
END_MESSAGE_MAP()


// SDTDlg 消息处理程序


BOOL SDTDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	m_cbx.InsertString(0,_T("R"));
	m_cbx.InsertString(1,_T("G"));
	m_cbx.InsertString(2,_T("B"));
	m_cbx.SetCurSel(0);


	m_cby.InsertString(0, _T("R"));
	m_cby.InsertString(1, _T("G"));
	m_cby.InsertString(2, _T("B"));
	m_cby.SetCurSel(0);


	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void SDTDlg::OnBnClickedButtonLayout()
{
	int tt = m_check.GetCheck();

	int xIndex = m_cbx.GetCurSel();
	int yIndex = m_cby.GetCurSel();

	UCHAR **Ximg = new UCHAR*[OImageC.pImgH];
	memset(Ximg, 0, OImageC.pImgH * sizeof(UCHAR*));
	UCHAR **Yimg = new UCHAR*[OImageC.pImgH];
	memset(Yimg, 0, OImageC.pImgH * sizeof(UCHAR*));
	if (xIndex == 0)
	{
		for (int i = 0; i < 256; i++)
		{
			Ximg = OImageC.etmImg->Rimg;
		}
	}
	else if (xIndex == 1)
	{
		for (int i = 0; i < 256; i++)
		{
			Ximg = OImageC.etmImg->Gimg;
		}
	}
	else if (xIndex == 2)
	{
		for (int i = 0; i < 256; i++)
		{
			Ximg = OImageC.etmImg->Bimg;
		}
	}

	if (yIndex == 0)
	{
		for (int i = 0; i < 256; i++)
		{
			Yimg = OImageC.etmImg->Rimg;
		}
	}
	else if (yIndex == 1)
	{
		for (int i = 0; i < 256; i++)
		{
			Yimg = OImageC.etmImg->Gimg;
		}
	}
	else if (yIndex == 2)
	{
		for (int i = 0; i < 256; i++)
		{
			Yimg = OImageC.etmImg->Bimg;
		}
	}

	CRect rect;
	GetDlgItem(IDC_STATIC_SDT)->GetClientRect(rect);

	pDC = this->GetDlgItem(IDC_STATIC_SDT)->GetDC();

	int pointx, pointy;

	int Tongji[256][256] = {0};

	for (int i = 0; i < OImageC.pImgH; i++)
	{
		UCHAR *xdata = Ximg[i];
		UCHAR *ydata = Yimg[i];
		for (int j = 0; j < OImageC.pImgW; j++)
		{
			Tongji[xdata[j]][ydata[j]]++;
			/*Tongji[xdata[j] + 1][ydata[j]]++;
			Tongji[xdata[j] - 1][ydata[j]]++;
			Tongji[xdata[j]][ydata[j] + 1]++;
			Tongji[xdata[j]][ydata[j] - 1]++;*/
		}
	}

	int Max = 0;

	for (int i = 0; i < 256; i++)
	{
		for (int j = 0; j < 256; j++)
		{
			if (Tongji[i][j] != 0)
			{
				if (Tongji[i][j] > Max)
				{
					Max = Tongji[i][j];
				}
			}
		}
	}

	int Max1 = (int)Max / 50;
	int Max2 = (int)Max / 20 ;
	int Max3 = (int)Max;

	CString a;
	a.Format("%d", Max);
	AfxMessageBox(a);
	int c1 = 0; 
	int c2 = 0;
	int c3 = 0;


	for (int i = 0; i < 256; i++)
	{
		for (int j = 0; j < 256; j++)
		{
			if (Tongji[i][j] != 0)
			{
				int pointx = i / (float)256 * (rect.right - rect.left + 1);
				pointx = (rect.right - rect.left + 1) - pointx;
				int pointy = j / (float)256 * (- rect.top + rect.bottom + 1);

				int R, G, B;

				if (Tongji[i][j] <= Max1)
				{
					R = 0;
					G = 0;
					B = 128 + (int)Tongji[i][j] / Max1 * 127;
					c1++;
				}
				else if (Tongji[i][j] > Max1 && Tongji[i][j] <= Max2)
				{
					R = 0;
					G = (int)(Tongji[i][j] - Max1) / (Max2 - Max1) * 255;
					B = 255 - G;
					c2++;
				}
				else if (Tongji[i][j] > Max2 && Tongji[i][j] <= Max3)
				{
					R = 155 + (int)(Tongji[i][j] - Max2) / (Max3 - Max2) * 100;
					G = 255 - R;
					B = 0;
					c3++;
				}
				else
				{
					AfxMessageBox("ERROR");
				}

				pDC->SetPixel(pointx, pointy,RGB(R,G,B));

			}
		}
	}
}
